package com.syc.mybatis.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.syc.mybatis.domain.Person;
import com.syc.mybatis.utils.MyBatisUtil;

public class PersonDao {

	public void deletePerson() {
		try {

			SqlSession session = MyBatisUtil.getSqlSession();
			Person person = new Person();
			person.setPid(19);

			session.delete(Person.class.getName() + ".deletePerson", person);

			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSqlSession();
		}
	}

	// 可变长参数,等同于数组
	public void deleteByArray(int... ids) {
		try {

			SqlSession session = MyBatisUtil.getSqlSession();

			session.delete(Person.class.getName() + ".deleteByArray", ids);

			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSqlSession();
		}
	}

	public void deleteByList(List<Integer> ids) {
		try {

			SqlSession session = MyBatisUtil.getSqlSession();

			session.delete(Person.class.getName() + ".deleteByList", ids);

			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSqlSession();
		}
	}

	public void dynamicInsert(String name, String nickname) {
		try {
			SqlSession session = MyBatisUtil.getSqlSession();

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name);
			map.put("nickname", nickname);

			session.insert(Person.class.getName() + ".dynamicInsert", map);

			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSqlSession();
		}
	}
}
